<?php

/**
 * 开放商户 - 银联码加机功能使用示例
 */

require_once __DIR__ . '/../../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Core\DemoConfig;
use JLPay\Services\Openmerch\Device\DeviceService;
use JLPay\Models\Openmerch\Device\UnionPayCodeAddRequest;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

// =================== 日志配置 =====================
$logger = Logger::getInstance();

// =================== 工具函数 =====================
function createConfig(): OrgConfig
{
    return DemoConfig::createConfig();
}

// =================== 数据构建函数 =====================
function buildUnionpayAddRequest(): UnionPayCodeAddRequest
{
    $request = new UnionPayCodeAddRequest();
    
    // 设置请求参数
    $request->setMerchNo("84940535193A006")
            ->setPrintMerchName("测试打印商户名称");
    
    return $request;
}

// =================== 示例函数 =====================
function unionpayAddDemo(): void
{
    global $logger;

    $logger->info("开始执行银联码加机示例");
    
    // 创建配置对象和设备管理服务
    $config = createConfig();
    $deviceService = new DeviceService($config);
    
    // 创建银联码加机请求
    $request = buildUnionpayAddRequest();
    
    // 发送请求 - 使用DemoRequestHandler
    $response = DemoRequestHandler::sendRequestAndHandleResponse(
        $deviceService, 
        $request, 
        "银联码加机",
        function($service, $request) {
            return $service->addUnionpayCode($request);
        }
    );
    
    // 处理响应结果
    if ($response && $response->isSuccess()) {
        $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
    
    $logger->info("银联码加机示例执行完毕");
}

// =================== 主程序 =====================
$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 银联码加机示例 开始 =====");
$logger->info("=" . str_repeat("=", 50));

unionpayAddDemo();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 银联码加机示例执行完毕 =====");
$logger->info("=" . str_repeat("=", 50)); 